#!/usr/bin/env python
# -*- encoding: utf-8 -*-
'''
@File    :   APP_SessionControl_0x10_test.py
@Time    :   2025/03/20 16:31:28
@Author  :   YangLiang 
@Version :   1.0
@Contact :   yangl@genvict.com
@License :   (C)Copyright 2022-2025
@Desc    :   None
'''

from pytest_assume.plugin import assume

import pytest
import logging
import time
from sympy import I
# from sympy import assuming
from commDriver.TS_CAN import *
import allure
import sys
from collections.abc import Iterable

from sqlalchemy import false
sys.dont_write_bytecode = True


ECUReset_Valid = [
    ("11 01", "51 01"),          # 有效复位请求
    ("11 81", ""),               # 有效复位请求
]

ECUReset_NRC13 = [
    ("11", "7F 11 13"),         # 缺失子功能
    ("11 01 00", "7F 11 13"),   # 带多余参数
    ("11 81 00", "7F 11 13"),   # 带多余参数
    ("11 81 00 11", "7F 11 13"),# 带多余参数
    ("11 01 00 11", "7F 11 13"),# 带多余参数
]

ECUReset_NRC12 = [
    ("11 00", "7F 11 12"),      # 无效子功能0x00
    ("11 02", "7F 11 12"),      # 无效子功能0x02
    ("11 03", "7F 11 12"),      # 无效子功能0x02
    ("11 04", "7F 11 12"),      # 无效子功能0x02
    ("11 05", "7F 11 12"),      # 无效子功能0x02
    ("11 80", "7F 11 12"),      # 无效子功能0x80
    ("11 82", "7F 11 12"),      # 无效子功能0x82
    ("11 83", "7F 11 12"),      # 无效子功能0x83
    ("11 84", "7F 11 12"),      # 无效子功能0x84
    ("11 85", "7F 11 12"),      # 无效子功能0x85
]


'''11服务物理寻址测试用例'''
class Test_APP_ECUReset_Phy(object):

    @allure.severity('normal')
    @allure.title('物理寻址默认会话下ECU复位测试')
    @allure.description('验证物理寻址模式ECU硬复位功能')
    @pytest.mark.parametrize('req,resp', ECUReset_Valid, ids=[f"Request {req}" for req, resp in ECUReset_Valid])
    def test_ECUReset_Default_Phy(self, action, Driver, req, resp):
        iStep = 1
        iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, req, resp, 1, iStep, False)

    @allure.severity('normal')
    @allure.title('物理寻址扩展会话下ECU复位测试')
    @allure.description('验证物理寻址模式ECU硬复位功能')
    @pytest.mark.parametrize('req,resp', ECUReset_Valid, ids=[f"Request {req}" for req, resp in ECUReset_Valid])
    def test_ECUReset_Extended_Phy(self, action, Driver, req, resp):
        iStep = 1
        iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, req, resp, 3, iStep, False)

    @allure.severity('normal')
    @allure.title('物理寻址默认会话ECU复位NRC13测试')
    @allure.description('验证长度不对的请求返回NRC13')
    @pytest.mark.parametrize('req,resp', ECUReset_NRC13, ids=[f"Request {req}" for req, resp in ECUReset_NRC13])
    def test_ECUReset_NRC13_Default_Phy(self, action, Driver, req, resp):
        iStep = 1
        iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, req, resp, 1, iStep, False)

    @allure.severity('normal')
    @allure.title('物理寻址扩展会话ECU复位NRC13测试')
    @allure.description('验证长度不对的请求返回NRC13')
    @pytest.mark.parametrize('req,resp', ECUReset_NRC13, ids=[f"Request {req}" for req, resp in ECUReset_NRC13])
    def test_ECUReset_NRC13_Extended_Phy(self, action, Driver, req, resp):
        iStep = 1
        iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, req, resp, 3, iStep, False)

    @allure.severity('normal')
    @allure.title('物理寻址默认会话ECU复位NRC12测试')
    @allure.description('验证子功能不对的请求返回NRC12')
    @pytest.mark.parametrize('req,resp', ECUReset_NRC12, ids=[f"Request {req}" for req, resp in ECUReset_NRC12])
    def test_ECUReset_NRC12_Default_Phy(self, action, Driver, req, resp):
        iStep = 1
        iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, req, resp, 1, iStep, False)

    @allure.severity('normal')
    @allure.title('物理寻址扩展会话ECU复位NRC12测试')
    @allure.description('验证子功能不对的请求返回NRC12')
    @pytest.mark.parametrize('req,resp', ECUReset_NRC12, ids=[f"Request {req}" for req, resp in ECUReset_NRC12])
    def test_ECUReset_NRC12_Extended_Phy(self, action, Driver, req, resp):
        iStep = 1
        iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, req, resp, 3, iStep, False)

    # @allure.severity('normal')
    # @allure.title('物理寻址默认会话ECU复位NRC7F测试')
    # @allure.description('验证默认会话下复位请求返回NRC7F')
    # def test_ECUReset_NRC7F_Default_Phy(self, action, Driver):
    #     iStep = 1
    #     iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, '11 01', '7F 11 7F', 1, iStep, False)

    @allure.severity('minor')
    @allure.title('物理寻址默认会话下ECU复位NRC22测试')
    @allure.description('验证车速>3kph时复位请求返回NRC22')
    def test_ECUReset_NRC22_Default_Phy(self, action, Driver):
        iStep = 1
        iStep = Driver['CAN'].SetVehicleSpeed(Driver, iStep, 8)
        iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, "11 01", "7F 11 22", 1, iStep, False)
        iStep = Driver['CAN'].SetVehicleSpeed(Driver, iStep, 0)

    @allure.severity('minor')
    @allure.title('物理寻址ECU复位NRC22测试')
    @allure.description('验证车速>3kph时复位请求返回NRC22')
    def test_ECUReset_NRC22_Extended_Phy(self, action, Driver):
        iStep = 1
        iStep = Driver['CAN'].SetVehicleSpeed(Driver, iStep, 8)
        iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, "11 01", "7F 11 22", 3, iStep, False)
        iStep = Driver['CAN'].SetVehicleSpeed(Driver, iStep, 0)

    @allure.severity('normal')
    @allure.title('物理寻址默认会话ECU复位NRC12 NRC13优先级测试')
    @allure.description('验证NRC12和NRC13的优先级')
    def test_ECUReset_NRC13AndNRC12_priority_Default_Phy(self, action, Driver):
        iStep = 1
        iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, '11 02 00', '7F 11 12', 1, iStep, False)

    @allure.severity('normal')
    @allure.title('物理寻址扩展会话ECU复位NRC12 NRC13优先级测试')
    @allure.description('验证NRC12和NRC13的优先级')
    def test_ECUReset_NRC13AndNRC12_priority_Extended_Phy(self, action, Driver):
        iStep = 1
        iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, '11 02 00', '7F 11 12', 3, iStep, False)

    @allure.severity('normal')
    @allure.title('物理寻址默认会话ECU复位NRC13 NRC22优先级测试')
    @allure.description('验证NRC13和NRC22的优先级')
    def test_ECUReset_NRC13AndNRC22_priority_Default_Phy(self, action, Driver):
        iStep = 1
        iStep = Driver['CAN'].SetVehicleSpeed(Driver, iStep, 8)
        iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, '11 01 00', '7F 11 13', 1, iStep, False)
        iStep = Driver['CAN'].SetVehicleSpeed(Driver, iStep, 0)

    @allure.severity('normal')
    @allure.title('物理寻址扩展会话ECU复位NRC13 NRC22优先级测试')
    @allure.description('验证NRC13和NRC22的优先级')
    def test_ECUReset_NRC13AndNRC22_priority_Extended_Phy(self, action, Driver):
        iStep = 1
        iStep = Driver['CAN'].SetVehicleSpeed(Driver, iStep, 8)
        iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, '11 01 00', '7F 11 13', 3, iStep, False)
        iStep = Driver['CAN'].SetVehicleSpeed(Driver, iStep, 0)

    @allure.severity('normal')
    @allure.title('物理寻址默认会话ECU复位NRC12 NRC22优先级测试')
    @allure.description('验证NRC12和NRC22的优先级')
    def test_ECUReset_NRC12AndNRC22_priority_Default_Phy(self, action, Driver):
        iStep = 1
        iStep = Driver['CAN'].SetVehicleSpeed(Driver, iStep, 8)
        iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, '11 02', '7F 11 12', 1, iStep, False)
        iStep = Driver['CAN'].SetVehicleSpeed(Driver, iStep, 0)

    @allure.severity('normal')
    @allure.title('物理寻址扩展会话ECU复位NRC12 NRC22优先级测试')
    @allure.description('验证NRC12和NRC22的优先级')
    def test_ECUReset_NRC12AndNRC22_priority_Extended_Phy(self, action, Driver):
        iStep = 1
        iStep = Driver['CAN'].SetVehicleSpeed(Driver, iStep, 8)
        iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, '11 02', '7F 11 12', 3, iStep, False)
        iStep = Driver['CAN'].SetVehicleSpeed(Driver, iStep, 0)

'''11服务功能寻址测试用例'''
class Test_APP_ECUReset_Func(object):

    @allure.severity('normal')
    @allure.title('功能寻址默认会话下ECU复位测试')
    @allure.description('验证功能寻址模式ECU硬复位功能')
    @pytest.mark.parametrize('req,resp', ECUReset_Valid, ids=[f"Request {req}" for req, resp in ECUReset_Valid])
    def test_ECUReset_Default_Func(self, action, Driver, req, resp):
        iStep = 1
        iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, req, resp, 1, iStep, False, 'UDSFun')

    @allure.severity('normal')
    @allure.title('功能寻扩展会话下ECU复位测试')
    @allure.description('验证功能寻址模式ECU硬复位功能')
    @pytest.mark.parametrize('req,resp', ECUReset_Valid, ids=[f"Request {req}" for req, resp in ECUReset_Valid])
    def test_ECUReset_Extended_Func(self, action, Driver, req, resp):
        iStep = 1
        iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, req, resp, 3, iStep, False, 'UDSFun')

    @allure.severity('normal')
    @allure.title('功能寻址默认会话下ECU复位NRC13测试')
    @allure.description('验证长度不对的请求返回NRC13')
    @pytest.mark.parametrize('req,resp', ECUReset_NRC13, ids=[f"Request {req}" for req, resp in ECUReset_NRC13])
    def test_ECUReset_NRC13_Default_Func(self, action, Driver, req, resp):
        iStep = 1
        iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, req, resp, 1, iStep, False, 'UDSFun')

    @allure.severity('normal')
    @allure.title('功能寻址扩展会话下ECU复位NRC13测试')
    @allure.description('验证长度不对的请求返回NRC13')
    @pytest.mark.parametrize('req,resp', ECUReset_NRC13, ids=[f"Request {req}" for req, resp in ECUReset_NRC13])
    def test_ECUReset_NRC13_Extended_Func(self, action, Driver, req, resp):
        iStep = 1
        iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, req, resp, 3, iStep, False, 'UDSFun')

    @allure.severity('normal')
    @allure.title('功能寻址默认会话下ECU复位NRC12测试')
    @allure.description('验证子功能不对的请求返回NRC12')
    @pytest.mark.parametrize('req,resp', ECUReset_NRC12, ids=[f"Request {req}" for req, resp in ECUReset_NRC12])
    def test_ECUReset_NRC12_Default_Func(self, action, Driver, req, resp):
        iStep = 1
        iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, req, '', 1, iStep, False, 'UDSFun')

    @allure.severity('normal')
    @allure.title('功能寻址扩展会话下ECU复位NRC12测试')
    @allure.description('验证子功能不对的请求返回NRC12')
    @pytest.mark.parametrize('req,resp', ECUReset_NRC12, ids=[f"Request {req}" for req, resp in ECUReset_NRC12])
    def test_ECUReset_NRC12_Extended_Func(self, action, Driver, req, resp):
        iStep = 1
        iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, req, '', 3, iStep, False, 'UDSFun')

    # @allure.severity('normal')
    # @allure.title('功能寻址默认会话ECU复位NRC7F测试')
    # @allure.description('验证默认会话下复位请求返回NRC7F')
    # def test_ECUReset_NRC7F_Default_Func(self, action, Driver):
    #     iStep = 1
    #     iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, '11 01', '', 1, iStep, False, 'UDSFun')

    @allure.severity('minor')
    @allure.title('功能寻址默认会话ECU复位NRC22测试')
    @allure.description('验证车速>3kph时复位请求返回NRC22')
    def test_ECUReset_NRC22_Default_Func(self, action, Driver):
        iStep = 1
        iStep = Driver['CAN'].SetVehicleSpeed(Driver, iStep, 8)
        iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, "11 01", "7F 11 22", 1, iStep, False, 'UDSFun')
        iStep = Driver['CAN'].SetVehicleSpeed(Driver, iStep, 0)

    @allure.severity('minor')
    @allure.title('功能寻址扩展会话ECU复位NRC22测试')
    @allure.description('验证车速>3kph时复位请求返回NRC22')
    def test_ECUReset_NRC22_Extended_Func(self, action, Driver):
        iStep = 1
        iStep = Driver['CAN'].SetVehicleSpeed(Driver, iStep, 8)
        iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, "11 01", "7F 11 22", 3, iStep, False, 'UDSFun')
        iStep = Driver['CAN'].SetVehicleSpeed(Driver, iStep, 0)

    @allure.severity('normal')
    @allure.title('功能寻址默认会话ECU复位NRC12 NRC13优先级测试')
    @allure.description('验证NRC12和NRC13的优先级')
    def test_ECUReset_NRC13AndNRC12_priority_Default_Func(self, action, Driver):
        iStep = 1
        iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, '11 02 00', '', 1, iStep, False, 'UDSFun')

    @allure.severity('normal')
    @allure.title('功能寻址扩展会话ECU复位NRC12 NRC13优先级测试')
    @allure.description('验证NRC12和NRC13的优先级')
    def test_ECUReset_NRC13AndNRC12_priority_Extended_Func(self, action, Driver):
        iStep = 1
        iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, '11 02 00', '', 3, iStep, False, 'UDSFun')

    @allure.severity('normal')
    @allure.title('功能寻址默认会话ECU复位NRC13 NRC22优先级测试')
    @allure.description('验证NRC13和NRC22的优先级')
    def test_ECUReset_NRC13AndNRC22_priority_Default_Func(self, action, Driver):
        iStep = 1
        iStep = Driver['CAN'].SetVehicleSpeed(Driver, iStep, 8)
        iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, '11 01 00', '7F 11 13', 1, iStep, False, 'UDSFun')
        iStep = Driver['CAN'].SetVehicleSpeed(Driver, iStep, 0)

    @allure.severity('normal')
    @allure.title('功能寻址扩展会话ECU复位NRC13 NRC22优先级测试')
    @allure.description('验证NRC13和NRC22的优先级')
    def test_ECUReset_NRC13AndNRC22_priority_Extended_Func(self, action, Driver):
        iStep = 1
        iStep = Driver['CAN'].SetVehicleSpeed(Driver, iStep, 8)
        iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, '11 01 00', '7F 11 13', 3, iStep, False, 'UDSFun')
        iStep = Driver['CAN'].SetVehicleSpeed(Driver, iStep, 0)

    @allure.severity('normal')
    @allure.title('功能寻址默认会话ECU复位NRC12 NRC22优先级测试')
    @allure.description('验证NRC12和NRC22的优先级')
    def test_ECUReset_NRC12AndNRC22_priority_Default_Func(self, action, Driver):
        iStep = 1
        iStep = Driver['CAN'].SetVehicleSpeed(Driver, iStep, 8)
        iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, '11 02', '', 1, iStep, False, 'UDSFun')
        iStep = Driver['CAN'].SetVehicleSpeed(Driver, iStep, 0)

    @allure.severity('normal')
    @allure.title('功能寻址扩展会话ECU复位NRC12 NRC22优先级测试')
    @allure.description('验证NRC12和NRC22的优先级')
    def test_ECUReset_NRC12AndNRC22_priority_Extended_Func(self, action, Driver):
        iStep = 1
        iStep = Driver['CAN'].SetVehicleSpeed(Driver, iStep, 8)
        iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, '11 02', '', 3, iStep, False, 'UDSFun')
        iStep = Driver['CAN'].SetVehicleSpeed(Driver, iStep, 0)
